;

var request = require("request");
var helper = require("../../javascript/helper");
var db = require("../../javascript/mysql");

var mysql = new db.mysqlClient("accident");
var spider = function () {
    var that = this;
    //生成产品数据
    this.genProductInfo = function () {
        let projects = [];
        plan = {
            planA:{
                "year": ["0-17"],
                "days": ["1d", "2d", "4d", "8d", "11d", "14d", "17d", "21d", "25d", "30d"],
                "people_count": ["1"],
                "accident_death": ["100000"],
                "accident_medical": ["20000"],
                "serious_medical": ["20000"],
                "acute_death": ["30000"],
                "airplane_death": [""],
                "train_death": [""],
                "ship_death": [""],
                "passenger_death": [""]
            },
            planB:{
                "year": ["18-80"],
                "days": ["1d", "2d", "4d", "8d", "11d", "14d", "17d", "21d", "25d", "30d"],
                "people_count": ["1"],
                "accident_death": ["200000"],
                "accident_medical": ["20000"],
                "serious_medical": ["20000"],
                "acute_death": ["30000"],
                "airplane_death": ["200000"],
                "train_death": ["100000"],
                "ship_death": ["100000"],
                "passenger_death": ["100000"]
            }

        }
        
        for(k in plan){
            data = helper.product([plan[k]["year"],plan[k]["days"],plan[k]["people_count"],plan[k]["accident_death"],plan[k]["accident_medical"],plan[k]["serious_medical"],plan[k]["acute_death"],plan[k]["airplane_death"],plan[k]["train_death"],plan[k]["ship_death"],plan[k]["passenger_death"]])
            for( i =0; i<data.length;i++){
                dt = helper.formatProductInfo(data[i],plan[k])
                projects.push( dt );
            }
        }
        return projects;

    },
    this.httpRequest = function ( dt ) {

        if( dt.year == "0-17"){
            planCd = "nets0001"
        }else{
            planCd = "nets0002"

        }

        params = {
            "productNo": "16",
            "insuredSex": "",
            "insuredBirthday": "",
            "insuranceYears": dt["days"].replace("d",""),
            "paymentPeriod": "",
            "guaranteeAmount": "",
            "mult": "0",
            "payIntv": "0",
            "insuyearFlag": "D",
            "payendyearFlag": "",
            "contPlanCode": planCd
        }
        return new Promise(function (resolve, reject) {
            request({
                url: "https://www.ergo-life.cn//Admin/getCALCUData.do",
                method: "POST",
                json: false,
                rejectUnauthorized: false,
                body: helper.urlencode(params),
                headers: {
                    "Accept": "application/json, text/javascript, */*; q=0.01",
                    "Accept-Encoding": "gzip, deflate, br",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Connection": "keep-alive",
                    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                    "Cookie": "_ga=GA1.2.1753139979.1576025815; JSESSIONID=5E0DBABDD4B4338234F7E0EFA38898B6; _gid=GA1.2.19320675.1590457174",
                    "Sec-Fetch-Dest": "empty",
                    "Sec-Fetch-Mode": "cors",
                    "Sec-Fetch-Site": "same-origin",
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
                    "X-Requested-With": "XMLHttpRequest"
                }
            }, function (err, res, body) {

                if (!err && res.statusCode) {
                    if ( body.indexOf("result") !=-1){
                        price = Number(JSON.parse( body ).result).toFixed(2).toString();
                        rs = {
                            name:"境内长线游保险",
                            company:"德华安顾人寿",
                            price:price
                        };
                        
                        resolve( rs );
                        
                    }


                }
            })

        })

    },
    //执行函数
    this.main = async function(){
        data = that.genProductInfo();
        for( i=0;i<data.length;i++){
            dt = data[i]
            let rs = await that.httpRequest( dt );
            result = Object.assign(dt,rs);
            mysql.insertData( result );
            
        }
        mysql.close();
        
    }

}

